package com.gopay.bis.psbc.ebank.operation;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.psbc.ebank.PsbcB2cMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.ICertHelper;
import com.psbc.payment.client.SignatureService;

/**
 * @PsbcB2cCertHelper.java
 * @author Ryan
 * @2013-12-2 上午9:34:37 www.gopay.com.cn Inc.All rights reserved.
 */
public class PsbcB2cCertHelper implements ICertHelper {

    public String sign(BaseMessage baseMessage) throws GopayException {

        try {
            PsbcB2cMessage psbcB2cMessage  = (PsbcB2cMessage)baseMessage;
            String plainStr = psbcB2cMessage.getPlainMessage();
            BankLogUtils.printB2CSignPlainLog(psbcB2cMessage);
            String signMsg = SignatureService.sign(plainStr);
            psbcB2cMessage.setSignMessage(signMsg);
            BankLogUtils.printB2CSignSignatureLog(psbcB2cMessage, signMsg);
            return signMsg;
        } catch (Exception e) {
            BankLogUtils.printB2CSignExceptionLog(baseMessage, e);
            throw new GopayException(ProcCodeConstants.PROC_CODE_100E6029);
        }
    }

    public Object verify(BaseMessage baseMessage) throws GopayException {
        try {
            PsbcB2cMessage psbcB2cMessage  = (PsbcB2cMessage)baseMessage;
            BankLogUtils.printB2CVerifyPlainLog(psbcB2cMessage);
            boolean isVeriy = SignatureService.verify(psbcB2cMessage.getPlainMessage(), psbcB2cMessage.getSignMessage());
            BankLogUtils.printB2CVerifyResultLog(psbcB2cMessage, isVeriy);
            return isVeriy;
        } catch (Exception e) {
            BankLogUtils.printB2CVerifyExceptionLog(baseMessage, e);
            throw new GopayException(ProcCodeConstants.PROC_CODE_100E6031);
        }

    }

}
